package com.example.JcGQS.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.JcGQS.mapper.GoodsMapper;
import com.example.JcGQS.pojo.dto.DeleteCategoryDTO;
import com.example.JcGQS.pojo.dto.DeleteGoodsDTO;
import com.example.JcGQS.pojo.dto.InsertGoodsDTO;
import com.example.JcGQS.pojo.entity.Goods;
import com.example.JcGQS.pojo.entity.GoodsCategory;
import com.example.JcGQS.pojo.po.GoodsPo;
import com.example.JcGQS.pojo.vo.GoodsAttributeVo;
import com.example.JcGQS.pojo.vo.GoodsListVo;
import com.example.JcGQS.pojo.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName GoodsDao
 * @Description 物料dao
 *
 * @author 末日
 * @date 2023/11/21
 */
@Component
public class GoodsDao {

    @Autowired
    private GoodsMapper goodsMapper;


    /**
     * 新增物料
     */
    public void insertGoods(Goods goods){
        goodsMapper.insert(goods);
    }


    /**
     * 删除物料
     */
    public void deleteGoods(DeleteGoodsDTO deleteGoodsDTO){
        goodsMapper.deleteById(deleteGoodsDTO.getGoodsId());
    }

    /**
     * 搜索物料
     */
    public void searchGoods(String value){
        goodsMapper.searchGoods(value);
}

    /**
     *  查询物料列表
     */

    public List<GoodsListVo> goodsList(){
       return goodsMapper.goodsList();

    }


    /**
     * 根据id查询物料数量
     */
    public Integer goodsSelect(Integer id){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",id);
        return   goodsMapper.selectCount(queryWrapper);
    }

    /**
     * 根据id查询物料数据
     */
    public Goods goodsSelectId(Integer id){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",id);
        return   goodsMapper.selectById(queryWrapper);
    }
    /**
     * 根据id查询物料数据
     */
    public GoodsListVo goodVoSelectId(Integer id){
        return   goodsMapper.selectByIdGoodVo(id);
    }

    /**
     * 根据number查询物料数据
     */
    public GoodsListVo goodsSelectNumber(String number){
        return   goodsMapper.selectNumber(number);
    }

    /**
     * 根据name查询物料数据
     */
    public Integer goodsSelectName(Integer categoryId,String goodsName,String goodsSpecification,String goodsModel,String goodsUnit,String brand){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId);
        queryWrapper.eq("goods_name",goodsName);
        queryWrapper.eq("goods_specification",goodsSpecification);
                queryWrapper.eq("goods_model",goodsModel);
                queryWrapper.eq("goods_unit",goodsUnit);
                queryWrapper.eq("brand",brand);
        return   goodsMapper.selectCount(queryWrapper);
    }
    /**
     * 根据型号查询物料数据
     */
    public Integer goodsSelectGoodsModel(String goodsModel){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_model",goodsModel);
        return   goodsMapper.selectCount(queryWrapper);
    }



    /**
     * @Title listGoodsAttribute
     * @Description 查询物料属性
     * 
     * @param columnCode
     *            查询字段代号
     * @param goodsAttributeVo
     *            物料属性请求参数
     * @return List<GoodsVo>
     */
    public List<GoodsVo> listGoodsAttribute(String columnCode, GoodsAttributeVo goodsAttributeVo) {
        //System.out.println("========="+goodsAttributeVo.getCategoryId());
        return goodsMapper.listGoodsAttribute(columnCode, goodsAttributeVo);
    }

    /**
     * @Title saveGoods
     * @Description 添加物料信息 </br>
     *              相同物料信息只添加一次</br>
     *              返回物料信息id</br>
     *              判断相同条件：</br>
     *              物料类别</br>
     *              物料名称</br>
     *              物料规格</br>
     *              物料型号</br>
     *              物料单价</br>
     * 
     * @param goodsPo
     *            物料属性
     * @return Integer
     */
    public Integer saveGoods(GoodsPo goodsPo) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("goods_id");
        queryWrapper.eq("goods_category", goodsPo.getGoodsCategory());
        queryWrapper.eq("goods_name", goodsPo.getGoodsName());
        queryWrapper.eq("goods_specification", goodsPo.getGoodsSpecification());
        queryWrapper.eq("goods_model", goodsPo.getGoodsModel());
        queryWrapper.last("LIMIT 1");
        Goods goods = goodsMapper.selectOne(queryWrapper);
        // 如果未找到物料信息则进行添加
        if (Objects.isNull(goods)) {
            goods = new Goods();
            goods.setGoodsName(goodsPo.getGoodsName());
            goods.setGoodsSpecification(goodsPo.getGoodsSpecification());
            goods.setGoodsModel(goodsPo.getGoodsModel());
            goods.setGoodsUnit(goodsPo.getGoodsUnit());
            goodsMapper.insert(goods);
        }
        // 返回添加后的物料id
        return goods.getGoodsId();
    }

    /**
     * @Title getGoodsById
     * @Description 查询物料信息根据id 查询字段: </br>
     *              物料id</br>
     *              物料类别</br>
     *              物料名称</br>
     *              物料规格</br>
     *              物料型号</br>
     *              物料单价</br>
     *              物料单位</br>
     * 
     * @param goodsId
     *            物料id
     * @return Goods
     */
    public Goods getGoodsById(Integer goodsId) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId);
        queryWrapper.select("goods_id", "goods_category", "goods_name", "goods_specification", "goods_model",
            "goods_unitprice", "goods_unitprice", "goods_unit");
        queryWrapper.last("LIMIT 1");
        return goodsMapper.selectOne(queryWrapper);
    }

    /**
     * @Title updateGoods
     * @Description 更新物料信息
     * 
     * @param goodsPo
     *            新物料信息
     * @param goodsId
     *            物料id
     */
    public void updateGoods(GoodsPo goodsPo, Integer goodsId) {
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<Goods>();
        updateWrapper.eq("goods_id", goodsId);
        Goods goods = createGoods(goodsPo);
        goodsMapper.update(goods, updateWrapper);
    }
    
    /**
     * 更新物料
     *
     * @param goods 货物
     */
    public void updateGoods(Goods goods) {
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<Goods>();
        updateWrapper.eq("goods_id", goods.getGoodsId());
        goodsMapper.update(goods, updateWrapper);
    }


    /**
     * @Title createGoods
     * @Description 创建物料对象
     * 
     * @param goodsPo
     *            物料信息
     * @return Goods
     */
    private Goods createGoods(GoodsPo goodsPo) {
        Goods goods = new Goods();
        goods.setGoodsName(goodsPo.getGoodsName());
        goods.setGoodsSpecification(goodsPo.getGoodsSpecification());
        goods.setGoodsModel(goodsPo.getGoodsModel());

        goods.setGoodsUnit(goodsPo.getGoodsUnit());
        return goods;
    }

}
